<?php
class CasetypesController extends AppController{	
	var $casetype='';				
	public function getJson($value, $type='', $key=''){
		$this->view='Json';
		$data=array();
		if($type=='isFound'){
			if($key=='icd'){
				$casetype=$data['isFound']=$this->isIcdFound($value);
				if(count($casetype)>0){
					$data['isFound']=true;					
					$data['diagnosis']=$casetype['Casetype']['diagnosis'];
				}
				else{
					$data['isFound']=false;
				}								
			}
		}
		$this->set('data', $data);
		$this->set('json', 'data');
	}
	public function isIcdFound($icd){
		$data=$this->Casetype->findByIcd($icd);
		if($data==false){return false;}
		return $data;
	}
	public function getCaseType_($icdcode){
		$icd=explode(";", $icdcode);
		$casetype=array();		
		foreach($icd as $code){						
			$data=$this->Casetype->findByIcd($code);
			$casetype[]=$data;
		}		
		return $casetype;
	}
	public function getCaseType($icdcode){
		$icd=explode(";", $icdcode);
		$casetype="";
		foreach($icd as $code){
			$d=$this->_getcasetype($code);			
			$casetype.=$d.'<br>';			
			break;
		}		
		return $casetype;
	}
	public function getDescription($icdcode){				
		$icd=explode(";", $icdcode);
		$diagnosis=array();
		foreach($icd as $code){
			$d=$this->_getdescription($code);
			if(strlen($d)>0){
				//$diagnosis.='<label class=\'data\'>'.$d.'</label><br>';
				$diagnosis[]=$d;
			}
		}		
		return $diagnosis;		
	}
	public function _getdescription($icdcode){		
		$data=$this->Casetype->findByIcd($icdcode);
		$description=$data['Casetype']['diagnosis'];
		return $description;
	}	
	private function _getcasetype($icdcode){		
		$data=$this->Casetype->findByIcd($icdcode);
		$casetype=$data['Casetype']['casetype'];
		return $casetype;
	}
	public function add(){		
		$this->casetypes();	
	}
	private function casetypes(){	
		$sheet=2;
		App::import('Vendor','excel');	
		$data = new Spreadsheet_Excel_Reader("casetype.xls");			
		
		$rowcount=$data->rowcount($sheet);
		$colcount=$data->colcount($sheet);
			
		for($r=2; $r<=$rowcount; $r++){
			$icd="";$casetype="";$subcode="";$diagnosis="";$description="";
			for($c=1; $c<=$colcount; $c++){
				if($c==1){$casetype=$data->val($r, $c, $sheet);}
				else if($c==2){$icd=$data->val($r, $c, $sheet);}
				else if($c==3){$subcode=$data->val($r, $c, $sheet);}
				else if($c==4){$diagnosis=$data->val($r, $c, $sheet);}
				else if($c==5){$description=$data->val($r, $c, $sheet);}
			}			
			$this->data['Casetype']['icd']=$icd;
			$this->data['Casetype']['casetype']=$casetype;
			$this->data['Casetype']['subcode']=$subcode;
			$this->data['Casetype']['diagnosis']=$diagnosis;
			$this->data['Casetype']['description']=$description;
			
			$this->Casetype->create();
			$this->Casetype->save($this->data);		
		}
	}
}
?>